package com.tsdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsdp.dto.Result;
import com.tsdp.dto.UserDTO;
import com.tsdp.entity.Follow;
import com.tsdp.entity.User;
import com.tsdp.mapper.FollowMapper;
import com.tsdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsdp.service.IUserService;
import com.tsdp.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.swing.text.html.StyleSheet;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 范大晨
 * @since 2022-11-9
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private IUserService userService;

    @Override
    //关注 取关
    public Result follow(Long followUserId, Boolean isFollow) {
        Long id = UserHolder.getUser().getId();
        if (isFollow) {
            Follow follow = new Follow();
            follow.setUserId(id);
            follow.setFollowUserId(followUserId);
            boolean isSuccess = save(follow);
            if (isSuccess) {
                redisTemplate.opsForSet().add("follow:"+id,followUserId.toString());
            }
        } else {
            LambdaQueryWrapper<Follow> queryWrapper = new  LambdaQueryWrapper<>();
            queryWrapper.eq(Follow::getUserId, id).eq(Follow::getFollowUserId, followUserId);
            boolean isSuccess = remove(queryWrapper);
            if (isSuccess) {
                redisTemplate.opsForSet().remove("follow:"+id,followUserId);
            }
        }
        return Result.ok();
    }

    //是否关注
    @Override
    public Result isFollow(Long followUserId) {
        Long id = UserHolder.getUser().getId();
        LambdaQueryWrapper<Follow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Follow::getUserId, id).eq(Follow::getFollowUserId, followUserId);
        int count = count(queryWrapper);
        return Result.ok(count > 0);
    }

    @Override
    public Result common(Long followUserId) {
        Long id = UserHolder.getUser().getId();
        String key1="follow:"+id;
        String key2="follow:"+followUserId;
        Set<String> intersect = redisTemplate.opsForSet().intersect(key1, key2);
        if (intersect.isEmpty()||intersect==null) {
            return Result.ok(Collections.emptyList());
        }
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        List<UserDTO> users = userService.listByIds(ids).stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class)).collect(Collectors.toList());
        return Result.ok(users);

    }
}
